package com.cby.springboot.demo.controller;

import com.cby.springboot.demo.bean.Customer;
import com.cby.springboot.demo.bean.User;
import com.cby.springboot.demo.service.CustomerService;
import com.cby.springboot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DataController extends BaseController{

    //自动装配 jdbcTemplate
    @Autowired
    JdbcTemplate jdbcTemplate;
    /**
     * 访问"/testSql",访问数据库
     * @return
     */
    @ResponseBody
    @GetMapping("/testSql")
    public String testSql() {
        String SQL = "SELECT count(*) from `user`";
        Integer integer = jdbcTemplate.queryForObject(SQL, Integer.class);
        return integer.toString();
    }

    @Autowired
    CustomerService customerService;

    @ResponseBody
    @GetMapping("/queryData")
    public String queryData() {
        Customer customer = new Customer();
        customer.setCustomerId("101");
        Customer c = customerService.getByCustomerId(customer);
        return c.getCustomerName();
    }

    @Autowired
    UserService userService;

    @ResponseBody
    @GetMapping("/insertData")
    public String insertData() {
        User user = new User();
        user.setUserId("102");
        user.setUserName("kevin");
        user.setPassword("123456");
        user.setEmail("623672347@qq.com");
        int result = userService.insert(user);
        return result > 0? "插入成功":"插入失败";
    }



}
